﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Forums.Controllers
{
	using System.Collections.Generic;
	using System.Web.Mvc;
	using Falcon.Core.Controllers;
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;
	using Falcon.Forums.Domain;
	using Falcon.Forums.Domain.Enums;
	using Falcon.Forums.Domain.Services;
	using Falcon.Forums.Domain.Values;

	public class SystemForumPostController : DefaultController
	{
		public SystemForumPostController(
			IForumService forumService,
			IForumPostService forumPostService)
		{
			this.ForumService = forumService;
			this.ForumPostService = forumPostService;
		}

		public IForumService ForumService
		{
			get;
			private set;
		}

		public IForumPostService ForumPostService
		{
			get;
			private set;
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Update(ForumPostUpdateValue value)
		{
			var forum = this.ForumService.GetById(value.ForumId);

			if (!forum.CanUpdateAnyPost)
			{
				return NotAuthorized();
			}

			var posts = new List<ForumPost>();

			this.ForumPostService.Transform(value.PostsToUpdate, posts);

			switch (value.Action)
			{
				case ForumPostAction.Delete:
					{
						return View(Views.Delete, Masters.System, new ForumPostDelete(posts));
					}
				case ForumPostAction.DeleteSpam:
					{
						return View(ForumsViews.Spam, Masters.System, new ForumPostDeleteSpam(posts));
					}
				case ForumPostAction.Move:
					{
						return View(Views.Move, Masters.System, new ForumPostMove(posts));
					}
				case ForumPostAction.Merge:
					{
						return View(ForumsViews.Merge, Masters.System, new ForumPostMerge(posts));
					}
				case ForumPostAction.Copy:
					{
						return View(ForumsViews.Copy, Masters.System, new ForumPostCopy(posts));
					}
				default:
					{
						this.ForumPostService.Update(posts, value.Action);
					}
					break;
			}

			return Redirect(value.Redirect);
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Delete(ForumPostDelete value)
		{
			var forum = this.ForumService.GetById(value.ForumId);

			if (!forum.CanDeleteAnyPost)
			{
				return NotAuthorized();
			}

			this.ForumPostService.Delete(value);

			return Redirect(value.Redirect);
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Spam(ForumPostDeleteSpam value)
		{
			var forum = this.ForumService.GetById(value.ForumId);

			if (!forum.CanDeleteAnyPost)
			{
				return NotAuthorized();
			}

			this.ForumPostService.Delete(value);

			return Redirect(value.Redirect);
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Move(ForumPostMove value)
		{
			try
			{
				var forum = this.ForumService.GetById(value.ForumId);

				if (!forum.CanMoveAnyPost)
				{
					return NotAuthorized();
				}

				this.ForumPostService.Move(value);

				return Redirect(value.Redirect);
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			var posts = new List<ForumPost>();

			this.ForumPostService.Transform(value.PostsToMove, posts);

			value.Posts = posts;

			return View(Views.Move, Masters.System, value);
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Merge(ForumPostMerge value)
		{
			var forum = this.ForumService.GetById(value.ForumId);

			if (!forum.CanMoveAnyPost)
			{
				return NotAuthorized();
			}

			this.ForumPostService.Merge(value);

			return Redirect(value.Redirect);
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Copy(ForumPostCopy value)
		{
			try
			{
				var forum = this.ForumService.GetById(value.ForumId);

				if (!forum.CanMoveAnyPost)
				{
					return NotAuthorized();
				}

				this.ForumPostService.Copy(value);

				return Redirect(value.Redirect);
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			var posts = new List<ForumPost>();

			this.ForumPostService.Transform(value.PostsToCopy, posts);

			value.Posts = posts;

			return View(ForumsViews.Copy, Masters.System, value);
		}
	}
}